home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / texdraw / txdtools.tex < prev   
Text File  |  1992-07-06  |  5KB  |  146 lines

  1. % TeXdraw toolbox macros, useful for extended TeXdraw commands
  2.  
  3. % $Id: txdtools.tex,v 1.7 1992/07/07 14:42:13 kabal Rel $
  4.  
  5. %   Copyright (C) 1991,1992  Peter Kabal
  6.  
  7. % The routines in this file are provided free of charge without
  8. % warranty of any kind.  Note that the TeXdraw routines are copyrighted.
  9. % They may be distributed freely provided that the recipients also
  10. % acquire the right to distribute them freely.  The notices to this
  11. % effect must be preserved when the files are distributed.
  12.  
  13. %  Peter Kabal
  14. %  Department of Electrical Engineering
  15. %  McGill University
  16. %  3480 University
  17. %  Montreal, Quebec
  18. %  Canada  H3A 2A7
  19.  
  20. %  kabal@aldebaran.EE.McGill.CA
  21.  
  22. % ===============================================================
  23.  
  24. % These macros use temporary count registers defined by TeXdraw
  25. %  \t@counta     \t@pixa
  26. %  \t@countb     \t@pixb
  27. %  \t@countc     \t@pixc
  28. %                \t@pixd
  29.  
  30. \chardef\catamp=\the\catcode`\@
  31. \catcode`\@=11
  32.  
  33.  
  34. % ===== Real arithmetic
  35. % Real addition
  36. %  #1  - summand
  37. %  #2  - summand
  38. %  #3  - macro name to capture the real result
  39. \def\realadd #1#2#3{\dimen0=#1pt
  40.                     \dimen2=#2pt
  41.                     \advance \dimen0 by \dimen2
  42.                     \edef #3{\expandafter\c@lean\the\dimen0}}
  43.  
  44. % Real division
  45. %  #1 - numerator
  46. %  #2 - denominator (divisor)
  47. %  #3 - macro name to capture the real result
  48. \def\realdiv #1#2#3{\dimen0=#1pt
  49.                     \t@counta=\dimen0
  50.                     \dimen0=#2pt
  51.                     \t@countb=\dimen0
  52.                     \intdiv \t@counta \t@countb #3}
  53.  
  54. % ===== Integer arithmetic
  55.  
  56. % Length of the hypotenuse
  57. % Find the length of a vector, lenhyp = sqrt(dx*dx + dy*dy)
  58. %  #1 - integer value, dx
  59. %  #2 - integer value, dy
  60. %  #3 - count register to capture the integer value
  61. \def\lenhyp #1#2#3{\t@counta=#1%
  62.                    \multiply \t@counta by \t@counta
  63.                    \t@countb=#2%
  64.                    \multiply \t@countb by \t@countb
  65.                    \advance \t@counta by \t@countb
  66.                    \sqrtnum \t@counta #3}
  67.  
  68. % Square root of an integer
  69. % Newton-Raphson iteration to find the square root, integer argument
  70. % Let the current estimate of the square root of x be b(k).
  71. % Form an error function, e(k)=b(k)*b(k)-x. Follow the gradient of the
  72. % error to calculate the next guess,
  73. %
  74. %    e(k) - 0     d e(k)                          b(k) + x/b(k)
  75. %   ----------  = ------  = 2*b(k)  ==>  b(k+1) = -------------
  76. %   b(k)-b(k+1)   d b(k)                               2
  77. %
  78. % Note this iteration does not work for x=0, since the guess is then b(k)=0.
  79. % Rename the count registers to have more suggestive names
  80. \let\bk=\t@counta
  81. \let\bn=\t@countb
  82. \let\xval=\t@countc
  83. \def\sqrtnum #1#2{\xval=#1%
  84.                   \bk=\xval
  85.                   \loop
  86.                     \bn=\xval
  87.                     \divide \bn by \bk
  88.                     \advance \bn by \bk
  89.                     \advance \bn by 1            % rounding
  90.                     \divide \bn by 2
  91.                   \ifnum \bn < \bk
  92.                     \bk=\bn
  93.                   \repeat
  94.                   #2=\bn}
  95.  
  96. % ===== Coordinate macros
  97.  
  98. % Return the coordinates of the current position
  99. %  #1 - macro name to capture the x-coordinate
  100. %  #2 - macro name to capture the y-coordinate
  101. \def\currentpos #1#2{\t@pixa=\x@pix
  102.                      \advance \t@pixa by -\x@segoffpix
  103.                      \pixtocoord \t@pixa #1
  104.                      \t@pixa=\y@pix
  105.                      \advance \t@pixa by -\y@segoffpix
  106.                      \pixtocoord \t@pixa #2}
  107.  
  108. % Length of a vector
  109. % Find the length of the vector between coordinate (#1 #2) and
  110. % coordiante (#3 #4). The length is expressed relative to the
  111. % current scaling.
  112. %  (#1 #2) - vector start coordinates
  113. %  (#3 #4) - vector end coordinates
  114. %  #5 - macro name to receive the length
  115. \def\vectlen (#1 #2)(#3 #4)#5{\getpos (#1 #2)\x@arga\y@arga
  116.                               \getpos (#3 #4)\x@argb\y@argb
  117.                               \coordtopix \x@arga \t@pixa
  118.                               \coordtopix \x@argb \t@pixb
  119.                               \advance \t@pixb by -\t@pixa
  120.                               \coordtopix \y@arga \t@pixc
  121.                               \coordtopix \y@argb \t@pixd
  122.                               \advance \t@pixd by -\t@pixc
  123.                               \lenhyp \t@pixb \t@pixd \t@pixc
  124.                               \pixtocoord \t@pixc #5}
  125.  
  126. % Cossine and sine
  127. % Find the cosine and sine of the angle of a vector directed from
  128. % the coordinate (#1 #2) to the coordinate (#3 #4).
  129. %  (#1 #2) - start coordinates
  130. %  (#3 #4) - end coordinates
  131. %  #5 - macro name to receive the cosine of the angle
  132. %  #6 - macro name to receive the sine of the angle
  133. \def\cossin (#1 #2)(#3 #4)#5#6{\getpos (#1 #2)\x@arga\y@arga
  134.                                \getpos (#3 #4)\x@argb\y@argb
  135.                                \coordtopix \x@arga \t@pixa
  136.                                \coordtopix \x@argb \t@pixb
  137.                                \advance \t@pixb by -\t@pixa
  138.                                \coordtopix \y@arga \t@pixc
  139.                                \coordtopix \y@argb \t@pixd
  140.                                \advance \t@pixd by -\t@pixc
  141.                                \lenhyp \t@pixb \t@pixd \t@pixc
  142.                                \intdiv \t@pixb\t@pixc #5%
  143.                                \intdiv \t@pixd\t@pixc #6}
  144.  
  145. \catcode`\@=\catamp
  146.